package gew.data;

import gew.utils.SqlUtils;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TreeItem {

    private final static int LINK_TYPE_NONE = 0;
    private final static int LINK_TYPE_SIMPLE = 1;
    private final static int LINK_TYPE_SELECTED = 2;

    private final static int ID = 1;
    private final static int PARENT_ID = 2;
    private final static int CODE = 3;
    private final static int NAME = 4;
    private final static int LEVEL = 5;
    private final static int SELECTED = 6;
    private final static int EXPANDED = 7;
    private final static int URL_CODE = 8;
    private final static int URL_BEGIN = 9;
    private final static int URL_VAR = 10;
    private final static int URL_END = 11;

    private final int id;
    private final int parentId;
    private final String code;
    private final String name;
    private final int level;
    private final Byte selected;
    private final Byte expanded;
    private final String url_code;
    private final String url_begin;
    private final String url_var;
    private final String url_end;

    public TreeItem(ResultSet rs) throws SQLException {
        this.id = rs.getInt(ID);
        this.parentId = rs.getInt(PARENT_ID);
        this.code = rs.getString(CODE);
        this.name = rs.getString(NAME);
        this.level = rs.getInt(LEVEL);
        this.selected = rs.getObject(SELECTED) == null ? null : rs.getByte(SELECTED);
        this.expanded = rs.getObject(EXPANDED) == null ? null : rs.getByte(EXPANDED);
        this.url_code = rs.getString(URL_CODE);
        this.url_begin = rs.getString(URL_BEGIN);
        this.url_var = rs.getString(URL_VAR);
        this.url_end = rs.getString(URL_END);
    }

    public int getId() {
        return id;
    }

    public int getParentId() {
        return parentId;
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public int getLevel() {
        return level;
    }

    public Byte getSelected() {
        return selected;
    }

    public Byte getExpanded() {
        return expanded;
    }

    public String getUrl() {
        if (this.selected == 1) {
            String url = SqlUtils.excludeUrlCode(url_code, url_var);
            return url.isEmpty() ? "" : url_begin.concat(url).concat(url_end);
        } else {
            return url_begin.concat(SqlUtils.includeUrlCode(url_code, url_var)).concat(url_end);
        }
    }

    public int getLinkType() {
        return (this.selected == null)
                ? LINK_TYPE_NONE
                : (this.expanded == null ? LINK_TYPE_SIMPLE : LINK_TYPE_SELECTED);
    }

    public int getHasChild() {
        return this.expanded == null ? 0 : 1;
    }

}
